package com.iprojectmanager.views;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.iprojectmanager.entities.*;
import com.iprojectmanager.managers.*;

/**
 * @author Alucard
 */
public class VistaOperacionUsuario extends Vista
{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        if(request.getParameter("operacionesusuarios") != null)
        {
            if(request.getParameter("operacionesusuarios").compareTo("Crear") == 0)
            {
                mainView(response.getWriter(),request);
            }else
            {
                chooseUser(response.getWriter(),request.getParameter("operacionesusuarios"),request);
            }
        }
        /*if(request.getParameter("operacionesintegrante") != null)
        {
            if(request.getParameter("operacionesusuarios").compareTo("Crear") == 0)
            {
                mainView(response.getWriter());
            }else
            {
                chooseUser(response.getWriter(),request.getParameter("operacionesusuarios"));
            }
        }*/
        else if(request.getParameter("usuario") != null)
        {
            
            if(request.getParameter("operacion") != null &&
               request.getParameter("operacion").compareTo("Asignar Roles") == 0)
            {
                assignRolView(request,response,"operacion");
            }
            else if(request.getParameter("operacion") != null &&
                    request.getParameter("operacion").compareTo("Modificar") == 0)
            {
                modifyView(response.getWriter(),request.getParameter("usuario"),request);
            }else if(request.getParameter("operacionesintegrante") != null &&
                     request.getParameter("operacionesintegrante").compareTo("Asignar Roles") == 0)
            {
                assignRolView(request,response,"operacionesintegrante");
            }
        }
    }
    

    //para crear usuarios
    public void mainView(PrintWriter out,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");
        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Creacion De Usuarios",root));
        
        String form = 
        	"<div id=\"pageContent\">"+
                        "<div id=\"selection1\">" + this.getCrearUsuario() + "</div>" +
                        Utilities.getFooter() + 
                "</div>";
        
        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }

    public void modifyView(PrintWriter out,String id,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");
        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Modificacion De Usuarios",root));
        
        String form = 
        	"<div id=\"pageContent\">"+
                        "<div id=\"selection1\">" + this.getModificarUsuario(id) + "</div>" +
                        Utilities.getFooter() + 
                "</div>";
        
        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }
    
    public void assignRolView(HttpServletRequest request, HttpServletResponse response,String o)throws ServletException, IOException
    {
        HttpSession user = request.getSession();
        Usuario root = (Usuario)user.getAttribute("user");
        PrintWriter out = response.getWriter();
        UserManager manager = new UserManager();
        int id = Integer.valueOf(request.getParameter("usuario"));
        IntegranteProyecto usuario = (IntegranteProyecto)manager.getUsuario(id);
        String op = request.getParameter(o);
        
            out.println(Utilities.getDocType());
            out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
            out.println("<div id=\"container\">");
            out.println(Utilities.getIntro("Modificacion De Usuarios",root));

            String form = 
                    "<div id=\"pageContent\">"+
                            "<div id=\"selection1\">" + this.getEscogerRoles(usuario,op) + "</div>" +
                            Utilities.getFooter() + 
                    "</div>";

            out.println(form);
            out.println(Utilities.getMenu());
            out.println("</div> <!-- fin container---></BODY></HTML>");
            out.close();
    }
    
    public void chooseUser(PrintWriter out,String op,HttpServletRequest request)
    {
        boolean flag = (op.compareTo("Asignar Roles")==0)?false:true;
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");

        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Eleccion De Usuarios",root));
        
        String form = 
        	"<div id=\"pageContent\">"+
                        "<div id=\"selection1\">" + this.getEscogerUsuario(op,flag) + "</div>" +
                        Utilities.getFooter() + 
                "</div>";
        
        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }

    @Override
    public String getServletInfo()
    {
        return "Vista de ABC de Usuarios";
    }
    
    public String getCrearUsuario()
    {
        return
                "<form action=\"UsersControl\" method=\"GET\">" +
                    "<h3><span>Id. de usuario</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbIdUsuario\" size=\"10\" readonly>" +
                    "</span></p>" +
                    "<h3><span>Login</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbLoginUsuario\" size=\"20\">" +
                    "</span></p>" +
                    "<h3><span>Password</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"password\" name=\"tbPasswordUsuario\" size=\"20\">" +
                    "</span></p>" +
                    "<h3><span>Nombre</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbNombreUsuario\" size=\"30\">" +
                    "</span></p>" +
                    "<h3><span>Apellido paterno</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbApUsuario\" size=\"30\">" +
                    "</span></p>" +
                    "<h3><span>Apellido materno</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbAmUsuario\" size=\"30\">" +
                    "</span></p>" +
                    "<h3><span>E-mail</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbEmailUsuario\" size=\"30\">" +
                    "</span></p>" +
                    "<h3><span>Telefono</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbTelUsuario\" size=\"20\">" +
                    "</span></p>" +
                    "<h3><span>Celular</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbCelUsuario\" size=\"20\">" +
                    "</span></p>" +
                    "<h3><span>Direccion</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<textarea rows=\"5\" name=\"taDirUsuario\" cols=\"20\"> </textarea>" +
                    "</span></p>" +
                    "<h3><span>Tipo</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<select name=\"tipo\">" +
                            "<option>Integrante</option>" +
                            "<option>Lider de Proyecto</option>" +
                        "</select>" +
                    "</span></p>"  +
                    "<h3><span>Cambios</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"submit\" value=\"Guardar\" name=\"CA\">" +
                        "<input type=\"reset\" value=\"Reset\" name=\"CC\">" +
                    "</span></p>" +
                "<form>";
    }

    public String getModificarUsuario(String id)
    {
        UserManager manager = new UserManager();
        Usuario user = manager.getUsuario(Integer.valueOf(id));

        return
            "<form action=\"UsersControl\" method=\"GET\">" +
                "<h3><span>Id. de usuario</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbIdUsuario\" size=\"10\" readonly value=\"" + user.getId() + "\">" +
                "</span></p>" +
                "<h3><span>Login</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbLoginUsuario\" size=\"20\"value=\""+ user.getLogin() + "\">" +
                "</span></p>" +
                "<h3><span>Password</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"password\" name=\"tbPasswordUsuario\" size=\"20\" value=\"" + user.getPassword() + "\">" +
                "</span></p>" +
                "<h3><span>Nombre</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbNombreUsuario\" size=\"30\" value=\"" + user.getNombre() + "\">" +
                "</span></p>" +
                "<h3><span>Apellido paterno</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbApUsuario\" size=\"30\" value=\"" + user.getApellidoPaterno() + "\">" +
                "</span></p>" +
                "<h3><span>Apellido materno</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbAmUsuario\" size=\"30\" value=\"" + user.getApellidoMaterno() + "\">" +
                "</span></p>" +
                "<h3><span>E-mail</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbEmailUsuario\" size=\"30\" value=\"" + user.getEMail() + "\">" +
                "</span></p>" +
                "<h3><span>Telefono</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbTelUsuario\" size=\"20\" value=\"" + user.getTelefono() + "\">" +
                "</span></p>" +
                "<h3><span>Celular</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbCelUsuario\" size=\"20\" value=\"" + user.getCelular() + "\">" +
                "</span></p>" +
                "<h3><span>Direccion</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<textarea rows=\"5\" name=\"taDirUsuario\" cols=\"20\" value=\"" + user.getDireccion() + "\">"+ user.getDireccion() +"</textarea>" +
                "</span></p>" +
               "<h3><span>Tipo</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<select name=\"tipo\">" +
                            "<option"+ ((user instanceof IntegranteProyecto)?" selected ":"") +">Integrante</option>" +
                            "<option"+ ((user instanceof LiderProyecto)?" selected ":"") +">Lider de Proyecto</option>" +
                        "</select>" +
                    "</span></p>"  +
                "<h3><span>Cambios</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"submit\" value=\"Guardar\" name=\"MA\">" +
                    "<input type=\"reset\" value=\"Reset\" name=\"MC\">" +
                "</span></p>" +
            "<form>";
    }

    //Aqui recibe true si se va a modificar cualquier usuario y 
    //false si se van a asignar roles
    public String getEscogerUsuario(String op,boolean flag)
    {
        UserManager manager = new UserManager();
        ArrayList<Usuario> users = (flag)?manager.getUsuarios():manager.getIntegrantes(0);
        String table = "";

            for(Usuario user : users)
            {
                table +=
                    "<tr>" +
                        "<td><input name=\"usuario\" value=\""+ user.getId() +"\" type=\"checkbox\" size=\"20\"></td>" +
                    "<td>"+ user.getId() +"</td>"+
                    "<td>"+ user.getNombre() + " " +
                            user.getApellidoPaterno() + " " +
                            user.getApellidoMaterno() + "</td>"+
                    "</tr>";
            }
        
        return
                "<h3><span>Seleccione Algun Usuario</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<form action=\"UsersControl\" method=\"GET\">" +
                        "<Table border=\"1\" cellspacing=\"1\">" +
                            "<tr>" +
                                "<b><th>Elegir</th><th>Id</th><th>Usuario</th></b>" +
                            "</tr>" +
                                table +
                        "</Table>" +
                        "<br></br>" +
                        "<button type=\"submit\" value=\"" + op + "\" name=\"operacion\">"+ op +"</button>" +
                     "</form>" +
                "</span></p>";
    }
        
    public String getEscogerRoles(IntegranteProyecto user,String op)
    {
        RolManager manager = new RolManager();
        ArrayList<Rol> roles= manager.getRoles();
        String table = "";

            for(Rol rol : roles)
            {
                table +=
                    "<tr>" +
                        "<td><input name=\"rol\" value=\""+ rol.getId() +"\" " +
                             ((user.getRoles().contains(rol))?" checked ":"")+
                            " type=\"checkbox\" size=\"20\">" +
                        "</td>" +
                        "<td>"+ rol.getId() +"</td>"+
                        "<td>"+ rol.getNombre() +"</td>"+
                    "</tr>";
            }
        
        return
            "<h3><span>Seleccione Algun Usuario</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<form action=\"UsersControl\" method=\"GET\">" +
                        "<h3><span>Id. de usuario</span></h3>" +
                        "<p class=\"p1\"><span>" +
                            "<input type=\"text\" name=\"tbIdUsuario\" size=\"10\" readonly value=\"" + user.getId() + "\">" +
                        "</span></p>" +
                        "<h3><span>Nombre</span></h3>" +
                        "<p class=\"p1\"><span>" +
                            "<input readonly type=\"text\" name=\"tbNombreUsuario\" size=\"30\" value=\"" + user.getNombre() + "\">" +
                        "</span></p>" +
                        "<h3><span>Seleccione los roles del usuario</span></h3>" +
                        "<p class=\"p1\"><span>" +
                            "<Table border=\"1\" cellspacing=\"1\">" +
                                "<tr>" +
                                    "<b><th>Elegir</th><th>Id</th><th>Usuario</th></b>" +
                                "</tr>" +
                                    table +
                            "</Table>" +
                            "<br></br>" +
                            "<button type=\"submit\" value=\"" + op + "\" name=\"Aceptar\">"+ op +"</button>" +
                        "</span></p>" +
                     "</form>" +
                "</span></p>";
    }
}
